############# ########### ####### ##### ### ##
#
# Copyright (C) 2010 The Hursk Group, LLC
#
# This program is free software: you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (See file named COPYING).  If not, see 
# <http://www.gnu.org/licenses/>.
# 
############# ########### ####### ##### ### ##

#!/usr/bin/perl
package pkginfo_parser;
use strict;
use English qw( -no_match_vars );

$INPUT_RECORD_SEPARATOR = "\n\n";
#$, = ' ';		# set output field separator
#$\ = "\n";		# set output record separator


our $FS = "\n"; #Set field separator
#SUBSEP=":"

sub process {
    my $path_to_patchdiag_file = shift;
    my $ip = shift;
    
    my %pkg_arr;
    
    open FH, "<", $path_to_patchdiag_file;
    line:
    while (<FH>) {
	chomp;	# strip record separator
	my $line = $_;
        my @fields = split($FS, $line, -1);
    
        my $pkginst;
        my $version;
        foreach my $field (@fields) {
            my @tmp_arr = split(/: \s+ /x, $field, -1);
            $tmp_arr[0] =~ s/\s+//g if $tmp_arr[0];#Take out any spaces
            $tmp_arr[1] =~ s/\s+//g if $tmp_arr[1];#Take out any spaces
            
            if ($tmp_arr[0] and $tmp_arr[0] =~ /PKGINST/) {
                $pkginst = $tmp_arr[1];
            }
            
            if ($tmp_arr[0] and $tmp_arr[0] =~ /VERSION/) {
                $version = $tmp_arr[1];
                #if (tmp_arr[1] == "PSTAMP") my_pstamp = tmp_arr[2]
                #if (tmp_arr[1] == "NAME") my_name = tmp_arr[2]
                #if (tmp_arr[1] == "FILES") next
                #if (debug == "2") print "tmp_arr[1]: "tmp_arr[1]"\ttmp_arr[2]: "tmp_arr[2]
                ;
            }
        }
        #if (debug == "2" ) print "my_pkginst: "my_pkginst"\tmy_version: "my_version"\tmy_pstamp: "my_pstamp
        #pkg_arr[my_pkginst,"pkginst"] = my_pkginst
        #pkg_arr[my_pkginst,"version"] = my_version
        $pkg_arr{$pkginst} = $pkginst . ':' . $version;
        #pkg_arr[my_pkginst,"pstamp"] = my_pstamp
        #pkg_arr[my_pkginst,"name"] = my_name
        #undef @tmp_arr;
    }
    close FH;
    
    if ($ip and $ip !~ /^\s*$/){
        $ip = $ip . "\t";
    }
    else {
        $ip = "\t";
    }
    
    my $return_string;
    foreach my $member (keys %pkg_arr) {
        #if (debug) { print "member: "members"\tpkg_arr[members]: "pkg_arr[members] } else print pkg_arr[members]
        #print "pkg_arr["members"]: "pkg_arr[members]
        $return_string .= $ip . $pkg_arr{$member} . "\n";
    }
    
    return $return_string;
}